package com.joker.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.joker.bean.Weather;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class WeatherUtil {

    public static String parseURL() throws IOException {
        String url1 = "https://d1.weather.com.cn/wap40/101280101.html?_=1620709783370";
            URL url = new URL(url1);
            URLConnection urlConnection = url.openConnection();

            // 以流的形式读出
            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
            String str = "";
            String line;
            while ((line = br.readLine()) != null){
                str += line;
            }
            str = str.replace("var wap40 = ", "");
            System.out.println(str);
            return str;
    }

    public static Map<Integer,Weather> getAllWeather(){
        String url1 = "https://d1.weather.com.cn/wap40/101280101.html?_=1620709783370";
        try {
            URL url = new URL(url1);
            URLConnection urlConnection = url.openConnection();

            // 以流的形式读出
            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
            String str = "";
            String line;
            while ((line = br.readLine()) != null){
                str += line;
            }
            str = str.replace("var wap40 = ", "");
            System.out.println(str);

            //Json 解析
            JSONObject jsonObject = JSONObject.parseObject(str);

            String msg1 = jsonObject.get("msg1").toString(); //信息1
            String msg2 = jsonObject.get("msg2").toString(); //信息2

            JSONArray jsonObject1 = jsonObject.getJSONArray("datas");

            Map<Integer, Weather> totalMap = new HashMap<>();
            for (int i = 0; i < 40; i++) {
                JSONObject object = jsonObject1.getJSONObject(i);
                Weather weather1 = new Weather();
                String week = object.get("week").toString();
                String max = object.get("max").toString();
                String min = object.get("min").toString();
                String weather = object.get("weather").toString();
                String w1_code = object.get("w1_code").toString();

                weather1.setWeek(week);
                weather1.setMax(max);
                weather1.setMin(min);
                weather1.setWeather(weather);
                weather1.setW1_code(w1_code);

                totalMap.put(i+1, weather1);
            }

            return totalMap;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static String getCity() throws IOException {

        String str = WeatherUtil.parseURL();

        //Json 解析
        JSONObject jsonObject = JSONObject.parseObject(str);

        String city = jsonObject.get("city").toString();

        JSONArray jsonObject1 = jsonObject.getJSONArray("datas");

        return city;

    }

}
